<?php
namespace caroltc\jenkinsapp\util;
use caroltc\jenkinsapp\library\Facade;

class Log
{
    /**
     * info 日志
     * @param $msg
     */
    public static function i($msg)
    {
        self::write('INFO', $msg);
    }

    /**
     * error 日志
     * @param $msg
     */
    public static function e($msg)
    {
        self::write('ERROR', $msg);
    }

    /**
     * 写入日志
     * @param $level
     * @param $msg
     */
    private static function write($level, $msg)
    {
        if (Facade::isDebugMode()) {
            $filename = Facade::config('log_path', '') . 'isv-debug-' . Facade::getDebugKey().'.log';
        } else {
            $filename =  Facade::config('log_path', '') . 'isv-' . date('Y-m-d').'.log';
        }
        $logFile = fopen($filename, "aw");
        fwrite($logFile, "[".$level . "][" . date("Y-m-d h:i:s") . "]" . $msg . "\n");
        fclose($logFile);
    }

    /**
     * 返回debug数据，并在获取之后删除之
     * @return bool|string
     */
    public static function getDebugData()
    {
        $filename = Facade::config('log_path', '') . 'isv-debug-' . Facade::getDebugKey().'.log';
        if (file_exists($filename)) {
            $data = file_get_contents($filename);
            @unlink($filename); // 删除debug文件
            return $data;
        }
        return '';
    }
}
